<?php

namespace app\user\controller;

use app\bbs\controller\BbsBaseController;
use app\common\service\MessageUserService;
use app\bbs\validate\MessageUserValidate;
use app\bbs\model\MessageUser;
use app\common\attributes\RandyValidate;
use app\common\attributes\Menu;
use think\annotation\Inject;
use think\response\Json;
use think\App;

class MessageController extends BbsBaseController
{

    protected MessageUser $model;
    #[Inject]
    protected MessageUserService $service;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MessageUser();
    }

    #[menu('查看用户消息通知列表')]
    public function index() : Json
    {
        $data = $this->getParams();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $extendQueryHelper = $this->buildparams();
        $list = $this->service->getMessageUserList($extendQueryHelper, $data);
        foreach ($list as $row) {
        }
        return $this->success($list);
    }

    #[menu('查看用户消息通知列表')]
    public function list()
    {
        $data = $this->getParams();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $extendQueryHelper = $this->buildparams();
        $extendQueryHelper->setIsPage(false);
        $list = $this->service->getMessageUserList($extendQueryHelper, $data);
        foreach ($list as $row) {
        }
        return $this->success($list);
    }


    #[menu('查看用户消息通知详情')]
    #[RandyValidate(validate: MessageUserValidate::class, scene: 'read')]
    public function read($message_user_id) : Json
    {
        $res = $this->service->getMessageUserItem($message_user_id);
        return $this->returnSuccess($res);
    }

    #[menu('删除用户消息通知')]
    #[RandyValidate(validate: MessageUserValidate::class, scene: 'del')]
    public function del($message_user_id) : Json
    {
        $res = $this->service->delMessageUserItem($message_user_id);
        return $this->returnSuccess($res);
    }

    #[menu('更新用户消息通知')]
    #[RandyValidate(validate: MessageUserValidate::class, scene: 'update')]
    public function update($message_user_id) : \think\response\Json
    {
        $data = $this->getParams();
        $res = $this->service->setMessageUserItem($message_user_id, $data);
        return $this->returnSuccess();
    }

    #[menu('新增用户消息通知')]
    #[RandyValidate(validate: MessageUserValidate::class, scene: 'save')]
    public function save() : Json
    {
        $data = $this->getParams();
        $res = $this->service->addMessageUserItem($data);
        return $this->returnSuccess($res);
    }

    #[menu('获取未读消息总数')]
    public function getTotal($type) : Json
    {
        $user_id = get_client_id();
        $res = $this->service->getTotal($user_id, $type);
        return $this->returnSuccess(['num' => $res]);
    }

    public function setIsRead($message_user_id)
    {
        $res = $this->service->setIsRead($message_user_id);
        return $this->returnSuccess($res);
    }

    public function setAllIsRead($user_id, $type)
    {
        $res = $this->service->setAllIsRead($user_id, $type);
        return $this->returnSuccess($res);
    }
}
